#include "serialport.h"
#include "wiringPi.h"
#include "wiringSerial.h"

#include <QDebug>

SerialPort::SerialPort(QThread *parent)
    : QThread(parent)
{
    m_isExit = false;
    qDebug() << "Serial::Serial(QThread *parent)";
    if((usart_fd = serialOpen("/dev/ttyAMA0",115200)) < 0)
    {
        qDebug() << "????????????????????????";
    }else
    {
        qDebug() << "!!!!!!!!!!!!!!!!!!!!!!!!!!";
    }
}

SerialPort::~SerialPort()
{
    serialClose(usart_fd);
}

void SerialPort::beginSerial()
{
    m_isExit = false;
    this->start();
}

void SerialPort::stopSerial()
{
    m_isExit = true;
    this->exit();
}

char SerialPort::recvData()
{
    return serialGetchar(usart_fd);
}

void SerialPort::sendData(quint8 c)
{
    serialPutchar(usart_fd, c);
}

void SerialPort::sendString(QString str)
{
    serialPrintf(usart_fd, str.toStdString().c_str());
}

void SerialPort::run()
{
    while(!m_isExit)
    {
        quint8 c = recvData();
        emit signalReadyRead(c);
        msleep(200);
    }
}
